//
//  VolumeInfoDelegate.m
//  cTypica
//
//  Created by David Kavanagh on 11/20/08.
//  Copyright 2008 directThought. All rights reserved.
//

#import "VolumeInfoDelegate.h"

@implementation VolumeInfoDelegate
@synthesize volumeInfo, formatter;

+ (id)delegate {
	VolumeInfoDelegate *ret = [self alloc];
	ret.formatter = [[NSDateFormatter alloc] init];
	[ret.formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
	return ret;
}

- (void) dealloc {
	[formatter release];
	[super dealloc];
}

// XML parser delegate methods
- (void)parser: (NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
	if ([elementName isEqualToString:@"volumeSet"]) {
		self.volumeInfo = [[NSMutableArray alloc] initWithCapacity:1];
		inAttachmentSet = NO;
	}
	else if ([elementName isEqualToString:@"item"]) {
		if (!inAttachmentSet) {
			item = [VolumeInfo alloc];
		}
		else {
			attachment = [AttachmentInfo alloc];
		}
	}
	else if ([elementName isEqualToString:@"attachmentSet"]) {
		inAttachmentSet = YES;
		item.attachmentSets = [[NSMutableArray alloc] initWithCapacity:1];
	}
}

- (void)parser: (NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	NSString *cleanString = [currentStringValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	[currentStringValue setString:@""];
	if ([elementName isEqualToString:@"volumeSet"]) {
	}
	else if ([elementName isEqualToString:@"item"]) {
		if (!inAttachmentSet) {
			[self.volumeInfo addObject:item];
			[item release];
			item = nil;
		}
		else {
			[item.attachmentSets addObject:attachment];
			[attachment release];
			attachment = nil;
		}
	}
	else if ([elementName isEqualToString:@"volumeId"]) {
		if (!inAttachmentSet) {
			item.volumeId = cleanString;
		}
		else {
			attachment.volumeId = cleanString;
		}
	}
	else if ([elementName isEqualToString:@"size"]) {
		item.volSize = cleanString;
	}
	else if ([elementName isEqualToString:@"snapshotId"]) {
		item.snapshotId = cleanString;
	}
	else if ([elementName isEqualToString:@"availabilityZone"]) {
		item.zone = cleanString;
	}
	else if ([elementName isEqualToString:@"status"]) {
		if (!inAttachmentSet) {
			item.status = cleanString;
		}
		else {
			attachment.status = cleanString;
		}
	}
	else if ([elementName isEqualToString:@"createTime"]) {
		item.createTime = [formatter dateFromString:cleanString];
	}
	else if ([elementName isEqualToString:@"attachmentSet"]) {
		inAttachmentSet = NO;
	}
	else if ([elementName isEqualToString:@"instanceId"]) {
		attachment.instanceId = cleanString;
	}
	else if ([elementName isEqualToString:@"device"]) {
		attachment.device = cleanString;
	}
	else if ([elementName isEqualToString:@"attachTime"]) {
		attachment.attachTime = [formatter dateFromString:cleanString];
		//[cleanString release];
	}
}
@end
